<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>Data-Table 表格</title>
    <link rel="stylesheet" href="/res/frame/layui/css/layui.css">
    <link rel="stylesheet" href="/res/frame/static/css/style.css">
    <link rel="icon" href="/res/frame/static/image/code.png">
</head>
<body class="body">

<!-- 工具集 -->
<div class="my-btn-box">
    <span class="fl">
        <a class="layui-btn btn-add btn-default" id="btn-refresh"><i class="layui-icon">&#x1002;</i></a>
        <a class="layui-btn btn-add btn-default" id="btn-add" onclick="add()">添加</a>
    </span>
    <span class="fl">
       <span class="fl">
        <span class="layui-form-label">设备名称：</span>
        <div class="layui-input-inline" style="float: left;">
            <input type="text" id='devName' autocomplete="off" placeholder="设备名称" class="layui-input">
        </div>
         <span class="layui-form-label">开始时间：</span>
        <div class="layui-input-inline" style="float: left;">
            <input type="text" id='beginDate' placeholder="yyyy-MM-dd" autocomplete="off" placeholder="开始时间"
                   class="layui-input">
        </div>
         <span class="layui-form-label">结束时间：</span>
        <div class="layui-input-inline" style="float: left;">
            <input type="text" id='endDate' autocomplete="off" placeholder="yyyy-MM-dd" placeholder="结束时间"
                   class="layui-input">
        </div>
       <button class="layui-btn" data-type="reload">搜索</button>
    </span>
    </span>
</div>

<!-- 表格 -->

<table class="layui-hide" id="devTable" lay-filter="devList"></table>
<table class="layui-hide" id="portTable" lay-filter="portList"></table>

<script type="text/javascript" src="/res/frame/layui/layui.js"></script>
<script type="text/javascript" src="/res/js/index.js"></script>
<script type="text/javascript">
    // layui方法
    layui.use(['table', 'form', 'layer', 'vip_table', 'cookie', 'laydate'], function () {

        // 操作对象
        var form = layui.form
            , table = layui.table
            , layer = layui.layer
            , vipTable = layui.vip_table
            , $ = layui.jquery,
            cookie = layui.cookie,
            laydate = layui.laydate;

        //日期选择框渲染
        laydate.render({
            elem: '#beginDate',
        });
        laydate.render({
            elem: '#endDate',
        });


        // 表格渲染
        var tableIns = table.render({
            elem: '#devTable'                  //指定原始表格元素选择器（推荐id选择器）
            , height: vipTable.getFullHeight()
            , cols: [[
                {type: 'numbers', fixed: 'left', title: '序号'}
                , {
                    fixed: 'left', title: '操作', align: 'center', width: 300, templet: function (d) {
                        return ret2(d) +
                            "<a class=\"layui-btn layui-btn-sm layui-btn-radius layui-btn-normal\" lay-event=\"edit\">编辑</a>\n" +
                            ret(d) +
                            "    <a class=\"layui-btn layui-btn-sm layui-btn-radius layui-btn-danger\" lay-event=\"del\">删除</a>"
                    }
                }
                , {field: 'devName', title: '设备名', align: 'center'}
                , {field: 'pkGuid', hide: true}
                , {field: 'devConId', hide: true}
                , {
                    field: 'devStatus', title: '设备状态', templet: function (d) {
                        return d.devStatus == 'ONLINE' ? "在线" : "不在线";
                    }, align: 'center'
                }
                , {field: 'devIp', title: '设备IP', align: 'center'}
                , {field: 'devMac', title: 'MAC地址', align: 'center'}
                , {field: 'devReadcommunity', title: '读团体', align: 'center'}
                , {field: 'devSnmpPort', title: '端口', align: 'center'}
                , {field: 'devDescor', title: '设备描述', align: 'center'}
                , {
                    field: 'devType', title: '设类型', width: 120, templet: function (d) {
                        return d.devType == "SwitchType" ? "交换机" : "摄像头";
                    }, align: 'center'
                }
                , {
                    field: 'devSysUpTime', title: '设备运行时长', templet: function (d) {
                        return formatSeconds(d.devSysUpTime / 100);
                    }, align: 'center'
                }
                , {field: 'devCreateDate', title: '添加时间', align: 'center'}
            ]]
            , id: 'tableReload'
            , url: '/dev/listByPage'
            , cellMinWidth: 80
            , method: 'post'
            , page: true
            , contentType: 'application/json'
            , request: {
                pageName: 'pageNum' //页码的参数名称，默认：page
                , limitName: 'pageSize' //每页数据量的参数名，默认：limit
            }
            , parseData: function (res) { //res 即为原始返回的数据
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.message, //解析提示文本
                    "count": res.data.total, //解析数据长度
                    "data": res.data.list //解析数据列表
                }
            }, response: {
                statusName: 'code' //规定数据状态的字段名称，默认：code
                , statusCode: "200" //规定成功的状态码，默认：0
                , msgName: 'msg' //规定状态信息的字段名称，默认：msg
                , countName: 'count' //规定数据总数的字段名称，默认：count
                , dataName: 'data' //规定数据列表的字段名称，默认：data
            }
            , loading: false
            , limits: [30, 60, 90, 150, 300]
            , limit: 30 //默认采用30
            , done: function (res, curr, count) {
            }
        });

        //搜索数据
        var active = {
            reload: function () {
                //执行重载
                table.reload('tableReload', {
                    page: {
                        curr: 1 //重新从第 1 页开始
                    }
                    , where: {
                        beginDate: $("#beginDate").val(),
                        endDate: $("#endDate").val(),
                        devName: $("#devName").val()
                    }
                });
            },
        };

        $('.my-btn-box .layui-btn').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });

        // 刷新
        $('#btn-refresh').on('click', function () {
            tableIns.reload();
        });

        table.on('rowDouble(devList)', function (obj) {
            if (obj.data.devType == "SwitchType") {
                openPort(obj)
            }
            obj.tr.addClass('layui-table-click').siblings().removeClass('layui-table-click');
        });


        table.on('tool(devList)', function (obj) { //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data //获得当前行数据
                , layEvent = obj.event; //获得 lay-event 对应的值
            if (layEvent === 'add') {
            } else if (layEvent === 'edit') {
                layer.confirm('确认编辑设备', function (index) {
                    edit(data.pkGuid)
                });
            } else if (layEvent === 'del') {
                layer.confirm('确认删除设备', function (index) {
                    del(data.pkGuid);
                });
            } else if (layEvent === 'addCon') {
                addCon(data.pkGuid, "DEV")
            } else if (layEvent === 'editCon') {
                editCon(data.devConId)
            }


        });


        function ret(d) {
            console.log(d);
            return d.devType == "SwitchType" ? (d.devConId != null && d.devConId != "") ? "<a class=\"layui-btn layui-btn-sm layui-btn-radius layui-btn-normal\" lay-event=\"editCon\">编辑阈值</a>" :
                "<a class=\"layui-btn layui-btn-sm layui-btn-radius layui-btn-normal\" lay-event=\"addCon\">添加阈值</a>\n" : ""
        }

        function ret2(d) {
            console.log(d);
            return d.devType == "SwitchType" ? "<a class=\"layui-btn layui-btn-sm layui-btn-radius layui-btn-danger\" lay-event=\"detail\">详情</a>" : "";
        }

        function del(pkGuid) {
            $.ajax({
                url: '/dev/del?pkGuid=' + pkGuid,
                type: "get",
                contentType: "application/json",
                success: function (result) {
                    console.log(result)
                    if (result.code === 200) {

                        layer.msg(result.msg);
                        tableIns.reload();
                    } else {
                        layer.msg(result.msg);
                        tableIns.reload();
                    }
                },
                error: function (XMLHttpRequest, textStatus, errorThrown) {
                    console.log(XMLHttpRequest.responseText);

                }
            });
        }

    });

    function GetQueryString(name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]);
        return null;
    }


    function add() {
        layer.open({
            title: '添加设备',
            type: 2,
            anim: 5,
            area: ['30%', '55%'],
            content: ['/res/html/admin/addDev.html', 'no'] //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
        });

    }

    function openPort(obj) {
        console.log(obj.data.pkGuid)

        layer.open({
            title: "设备:" + obj.data.devName + '----端口详情',
            type: 2,
            anim: 0,
            maxmin: true,
            isOutAnim: true,
            area: ['90%', '90%'],
            content: ['/res/html/admin/devPortList.html?devId=' + obj.data.pkGuid] //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
        });

    }

    function addCon(id, ConType) {
        layer.open({
            title: '添加阈值',
            type: 2,
            anim: 5,
            area: ['520px', '470px'],
            content: ['/res/html/admin/addCon.html?id=' + id + "&conType=" + ConType, 'no'] //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
        });
    }


    function edit(pkGuid) {
        layer.open({
            title: '编辑设备',
            type: 2,
            anim: 5,
            area: ['470px', '420px'],
            content: ['/res/html/admin/editDev.html?pkGuid=' + pkGuid, 'no'] //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
        });
    }

    function editCon(id) {
        layer.open({
            title: '编辑阈值',
            type: 2,
            anim: 5,
            area: ['520px', '470px'],
            content: ['/res/html/admin/editCon.html?pkGuid=' + id, 'no'] //这里content是一个URL，如果你不想让iframe出现滚动条，你还可以content: ['http://sentsin.com', 'no']
        });
    }

    function ref() {
        $('#btn-refresh').on('click', function () {
            tableIns.reload();
        });
    }

    function formatSeconds(value) {
        var theTime = parseInt(value);// 需要转换的时间秒
        var theTime1 = 0;// 分
        var theTime2 = 0;// 小时
        var theTime3 = 0;// 天
        if (theTime > 60) {
            theTime1 = parseInt(theTime / 60);
            theTime = parseInt(theTime % 60);
            if (theTime1 > 60) {
                theTime2 = parseInt(theTime1 / 60);
                theTime1 = parseInt(theTime1 % 60);
                if (theTime2 > 24) {
                    //大于24小时
                    theTime3 = parseInt(theTime2 / 24);
                    theTime2 = parseInt(theTime2 % 24);
                }
            }
        }
        var result = '';
        if (theTime > 0) {
            result = "" + parseInt(theTime) + "秒";
        }
        if (theTime1 > 0) {
            result = "" + parseInt(theTime1) + "分" + result;
        }
        if (theTime2 > 0) {
            result = "" + parseInt(theTime2) + "小时" + result;
        }
        if (theTime3 > 0) {
            result = "" + parseInt(theTime3) + "天" + result;
        }
        return result;
    }


</script>
<!-- 表格操作按钮集 -->


</body>
</html>